﻿using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal struct Ref8
    {
        private ushort rwFirst, rwLast;
        private ushort colFirst, colLast;
        public void Read(BinaryReader reader)
        {
            rwFirst = reader.ReadUInt16();
            rwLast = reader.ReadUInt16();
            colFirst = reader.ReadUInt16();
            colLast = reader.ReadUInt16();
        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(rwFirst);
            writer.Write(rwLast);
            writer.Write(colFirst);
            writer.Write(colLast);
        }
        public static explicit operator RangeAddress(Ref8 address)
        {
            Contract.Ensures(((Ref8U)Contract.Result<RangeAddress>()).Equals(address));
            return new RangeAddress(new CellAddress(address.rwFirst, address.colFirst),
                                    new CellAddress(address.rwLast, address.colLast));
        }

        public static explicit operator Ref8(RangeAddress address)
        {
            return new Ref8
                       {
                           rwFirst = (ushort)address.TopLeft.Row,
                           rwLast = (ushort)address.BottomRight.Row,
                           colFirst = (ushort)address.TopLeft.Column,
                           colLast = (ushort)address.BottomRight.Column
                       };
        }

    }
}